<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title></title>
    <script src="js/mui.min.js"></script>
    <script src="js/common.js"></script>
    <link href="css/mui.min.css" rel="stylesheet"/>
    <link rel="stylesheet" type="text/css" href="css/own.css"/>
    <style>
    	body{
    		background-color: #ffcc33;
    	}
    	#main_status{
    		display: none;
    	}
    </style>
    <script> 
    	
    function showWindow(){
    	
			 	mui.openWindow({
                			url: 'guide.html',
                       		 id: 'guide.html'
                	})
			 }
    	mui.init();
    	
    	//当前应用版本号
	  	var wgtVer=null;
		mui.plusReady(function(){
			if (mui.cacheUser.isLogin()){
				mui.openWindow("tzdr/quotation/quotationMain.html","quotationMain");
			}
//			mui.getJSON("manifest.json",null,function(data){
//				alert("manifest.json1234")
//				alert("data.plus.distribute.plugins.statics.umeng"+JSON.stringify(data.plus.distribute.plugins.statics.umeng));
//			});
			//状态栏设置
			setstatus();
			 if(plus.storage.getItem("guide")==null){
				 plus.storage.setItem("guide","12");
				 plus.storage.setItem("apk","57a29e6ce0f55a5a040013bd");
				 setTimeout(showWindow,1);		
			 	}else{
            		var statusR =  document.getElementById("main_status");
            		statusR.style.display = "block";
            		document.getElementById("regist").addEventListener("tap",function(){
						if(mui.checkNetwork()==false){
							mui.toast("当前网络不给力，请稍后再试"); 
							return;
						}
						mui.openWindow("tzdr/login/regist.html","regist");
					});
				document.getElementById("visit").addEventListener("tap",function(){
					if(mui.checkNetwork()==false){
						mui.toast("当前网络不给力，请稍后再试"); 
						return;
					}
//					mui.openWindow("tzdr/account/account.html","account");
					mui.openWindow("tzdr/quotation/quotationMain.html","quotationMain");
				});
				document.getElementById("login").addEventListener("tap",function(){
					if(mui.checkNetwork()==false){
						mui.toast("当前网络不给力，请稍后再试"); 
						return;
					}
					mui.openWindow("tzdr/login/login.html","login");
					});
            };
			 	
			//Android双击home键退出应用
			mui.back = function(){
               if(!first) {
					first = new Date().getTime(); 
					mui.toast('再按一次退出国际期货通');
					setTimeout(function() {
					first = null;
					}, 1000);
				}else{
					if (new Date().getTime() - first < 1000) {   
						plus.runtime.quit();  
					} 
				}
        	};
			
			// 获取本地应用资源版本号
			plus.runtime.getProperty(plus.runtime.appid,function(inf){
				wgtVer=inf.version;
				//检查更新版本 
				checkUpdate(function(){});
			});
		});
		
		//状态栏设置
		function setstatus(){
			if(plus.navigator.isFullscreen()){
				plus.navigator.setFullscreen(false);
			}
		};
		var dataVersion;
		// 检测更新
		function checkUpdate(_callback){
			//获取最新版本信息
			var paramUrl={
				url:"tzdr/login/login.html"
			}
			mui.app_request('/getVersion',{force:true},
				function(result){
					dataVersion=result.data.version;
					
					if (dataVersion.indexOf("Android") < 0) { // 版本格式不兼容，不能更新(以新版本配置是否包含'Android'关键字判断)
						return;
					}
					dataVersion = JSON.parse(dataVersion);
					updateApk1(_callback);
				},function(result){
					_callback();
					if(mui.cacheUser.isLogin()){
						mui.openWindow("tzdr/quotation/quotationMain.html");
					}
					return; 
				},paramUrl
    		);
}
	function updateApk1(_callback){
				//http://api.mzkqh.com/upload/H50967E1D_.apk
		var nowDate = new Date();
		var Android = dataVersion.Android;
		var iOS = dataVersion.iOS;
		var upDateAndroid = new Date(Android.date);
		var upDateiOS = new Date(iOS.date);
		if(plus.os.name == "Android"){
			if(Android.version > wgtVer){
				if(nowDate >= upDateAndroid){
					if(Android.isForceUpdate == true){
						plus.nativeUI.alert("本次升级有较大更新，请务必重新下载安装！", function(){
							if(Android.bigUrl == null || Android.bigUrl == "" || Android.bigUrl == undefined || Android.bigUrl.length == 0){
								downWgt(function(){
								if (mui.cacheUser.isLogin()){
									mui.openWindow("tzdr/quotation/quotationMain.html");
								}
								});	// 下载升级包
							}else{
								var _download_url = Android.bigUrl;
									plus.runtime.openURL(_download_url);
							}
						
						}, "软件升级", "立即下载" );
					}else{
						plus.nativeUI.confirm("检测到有新版本需要升级，是否升级到最新版本?", function(e){
							if(e.index==0){
								if(Android.bigUrl=="" || Android.bigUrl.length==0 || Android.bigUrl==null){
									downWgt(function(){
										if (mui.cacheUser.isLogin()){
											mui.openWindow("tzdr/quotation/quotationMain.html");
										}
									});	// 下载升级包
								}else{
									var _download_url = Android.bigUrl;
									plus.runtime.openURL(_download_url);
								}
								
							}else{
								if (mui.cacheUser.isLogin()){
									mui.openWindow("tzdr/quotation/quotationMain.html");
								}
							}
						}, "软件升级", ["升级","稍后再说"] );	
					}
				}
			}
		}else if(plus.os.name == "iOS"){
			if(iOS.version > wgtVer){
				if(nowDate >= upDateiOS){
					if(iOS.isForceUpdate == true){
						plus.nativeUI.alert("本次升级有较大更新，请务必重新下载安装！", function(){
							if(iOS.bigUrl == null || iOS.bigUrl == "" || iOS.bigUrl == undefined || iOS.bigUrl.length == 0){
								downWgt(function(){
								if (mui.cacheUser.isLogin()){
									mui.openWindow("tzdr/quotation/quotationMain.html");
								}
								});	// 下载升级包
							}else{
								var _download_url = iOS.bigUrl;
									plus.runtime.openURL(_download_url);
							}
						
						}, "软件升级", "立即下载" );
					}else{
						plus.nativeUI.confirm("检测到有新版本需要升级，是否升级到最新版本?", function(e){
							if(e.index==0){
								if(iOS.bigUrl=="" || iOS.bigUrl.length==0 || iOS.bigUrl==null){
									downWgt(function(){
										if (mui.cacheUser.isLogin()){
											mui.openWindow("tzdr/quotation/quotationMain.html");
										}
									});	// 下载升级包
								}else{
									var _download_url = iOS.bigUrl;
									plus.runtime.openURL(_download_url);
								}
								
							}else{
								if (mui.cacheUser.isLogin()){
									mui.openWindow("tzdr/quotation/quotationMain.html");
								}
							}
						}, "软件升级", ["升级","稍后再说"] );	
					}
				}else{
					
				}
			}
		}
	};
	
		// 下载wgt文件
		function downWgt(_callback,data){
			plus.nativeUI.showWaiting("下载升级包..."); 
			plus.downloader.createDownload(dataVersion.Android.littleUrl, {filename:"_doc/update/"}, function(d,status){
				plus.nativeUI.closeWaiting();
				if (status == 200) { 
					installWgt(d.filename,_callback);	// 安装wgt包
				}else{
					// 弹出系统提示对话框
					plus.nativeUI.alert( "下载升级包失败！如多次失败请到官网下载最新APP。", function(){
						_callback();
					}, "软件升级", "确定" );
				}
			}).start();
		}
		
		// 更新应用资源
		function installWgt(path,_callback){
			plus.nativeUI.showWaiting("软件升级..."); 
			plus.runtime.install(path,{force:true},function(){
				plus.nativeUI.closeWaiting();
				plus.runtime.restart();
			},function(e){
				plus.nativeUI.closeWaiting();
				// 弹出系统提示对话框
				plus.nativeUI.alert( "应用升级失败！如多次失败请到官网下载最新APP。", function(){
					_callback();
				}, "软件升级", "确定" );
			});
		}
    </script>
</head>
<body>
<div class="mui-content main_page" id = "main_status">
	<img src="images/icons/LOGO.png" />
	<div class="main_link">
		<a href="javascript:void(0);" id="login" class="main_login">登录</a>
		<a href="javascript:void(0);" id="regist" class="main_regist">注册</a>
		<a href="javascript:void(0);" id="visit" class="main_other">先随便逛逛 ></a>
	</div>
	<p>Copyright © 2016 成都盈透科技有限公司 版权所有</p>
</div>
<script src="js/update.js"></script>
</body>
</html>
